<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <todo-header @addfn="addfn" :arr="list"></todo-header>
    <todo-main @delfn="delfn" :arr="showarr"></todo-main>
    <todo-footer
      @clearall="clearall"
      @chengefn="chengefn"
      :arr="showarr"
    ></todo-footer>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

import "./styles/base.css";
import "./styles/index.css";
export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      iscon: "all",
      list: JSON.parse(localStorage.getItem("list")) || [],
    };
  },
  methods: {
    addfn(name) {
      this.list.push({
        id:
          this.list.length == 0 ? 100 : this.list[this.list.length - 1].id + 1,
        name: name,
        isDone: false,
      });
    },
    delfn(index) {
      this.list.splice(index, 1);
    },
    chengefn(str) {
      this.iscon = str;
    },
    clearall() {
      this.list = this.list.filter((item) => item.isDone == false);
    },
  },
  computed: {
    showarr() {
      if (this.iscon == "ok") {
        return this.list.filter((item) => item.isDone == true);
      } else if (this.iscon == "no") {
        return this.list.filter((item) => item.isDone == false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler(newval) {
        localStorage.setItem("list", JSON.stringify(newval));
      },
      deep: true,
    },
  },
};
</script>